package com.lqs.sofp.dao.impl;

import com.lqs.sofp.dao.ManagerInfoDao;
import com.lqs.sofp.entity.Manager;
import com.lqs.sofp.utils.DbUtils;
import java.sql.*;
import java.util.ArrayList;
/**
 * @author ljy
 */
public class ManagerInfoDaolmpl implements ManagerInfoDao {
    @Override
    public boolean update (Manager manager){
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;

        try {
            String sql = "INSERT INTO ManagerInfo(Manager,Password) VALUES(?,?);";
            // 连接
            conn = DbUtils.getConnection();
            // 获取对象
            stmt = conn.prepareStatement(sql);
            stmt.setString(1,manager.getManager());
            stmt.setString(2,manager.getPassword());

            int num = stmt.executeUpdate();
            if (num > 0) {
                System.out.println("——添加成功——");
                return true;
            } else {
                System.out.println("——添加失败——");
                return false;
            }

        }catch (SQLException e){
            e.printStackTrace();
            System.out.println("——添加失败——");
            return false;
        } finally {
            DbUtils.release(rs, stmt, conn);
        }
    }

    @Override
    public boolean delete(String manager){
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            String sql = "delete from ManagerInfo where Manager=?";
            // 连接
            conn = DbUtils.getConnection();
            // 获取对象
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, manager);
            int num = stmt.executeUpdate();
            if (num > 0) {
                System.out.println("——成功删除——");
                return true;
            } else {
                System.out.println("——删除失败——");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbUtils.release(rs, stmt, conn);
        }
        return false;
    }

    @Override
    public Manager selsect(String manager){
        Manager manger=null;
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            conn = DbUtils.getConnection();
            String sql = "SELECT *FROM ManagerInfo where Manager=?";
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, manager);
            rs=stmt.executeQuery();

            //用来获取应该返回的结果
            rs.last();
            int count = rs.getRow();
            //System.out.println(count);//测试找到的行数
            rs.beforeFirst();

            if(count!=0){
                while (rs.next()){
                    String admin=rs.getString("Manager");
                    String password=rs.getString("Password");
                    Manager temp=new Manager(admin,password);
                    manger=temp;
                    //System.out.println(manger.getManager()+" "+manger.getPassword());//测试返回的数据。
                }
            }
        }catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbUtils.release(rs, stmt, conn);
        }
        return manger;
    }

    @Override
    public ArrayList<Manager>selectAll(){
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        ArrayList<Manager> managerlist=new ArrayList<>();

        try{
            String sql = "select *from ManagerInfo";
            // 连接
            conn = DbUtils.getConnection();
            // 获取对象
            stmt = conn.prepareStatement(sql);
            rs = stmt.executeQuery();
            while(rs.next()){
                String admin=rs.getString("Manager");
                String password=rs.getString("Password");
                Manager manager=new Manager(admin, password);
                managerlist.add(manager);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbUtils.release(rs, stmt, conn);
        }
        return managerlist;
    }
    @Override
    public boolean modify(String manager, String password) {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            String sql = "update ManagerInfo set Password = ? where Manager = ?";
            // 连接
            conn = DbUtils.getConnection();
            // 获取对象
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, password);
            stmt.setString(2, manager);
            int num = stmt.executeUpdate();
            if(num>0) {
                System.out.println("——修改成功——");
                return true;
            }else{
                System.out.println("——修改失败——");
                return false;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbUtils.release(rs, stmt, conn);
        }
        return false;
    }
}
